package game.game2048;


import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class Game2048 {
    private int[][] matrix;
    private Random random;

    public Game2048() {
        matrix = new int[4][4];
        random = new Random();
        addNumber();
        addNumber();
    }

    // 在空白位置上添加数字方块
    private void addNumber() {
        int x, y;
        do {
            x = random.nextInt(4);
            y = random.nextInt(4);
        } while (matrix[x][y] != 0);
        matrix[x][y] = random.nextInt(2) == 0 ? 2 : 4;
    }

    // 将矩阵向左移动
    private void moveLeft() {
        for (int i = 0; i < 4; i++) {
            int[] row = matrix[i];
            int[] newRow = new int[4];
            int index = 0;
            for (int j = 0; j < 4; j++) {
                if (row[j] != 0) {
                    if (newRow[index] == 0) {
                        newRow[index] = row[j];
                    } else if (newRow[index] == row[j]) {
                        newRow[index] *= 2;
                        index++;
                    } else {
                        index++;
                        newRow[index] = row[j];
                    }
                }
            }
            matrix[i] = newRow;
        }
        addNumber();
    }

    // 将矩阵向右移动
    private void moveRight() {
        for (int i = 0; i < 4; i++) {
            int[] row = matrix[i];
            int[] newRow = new int[4];
            int index = 3;
            for (int j = 3; j >= 0; j--) {
                if (row[j] != 0) {
                    if (newRow[index] == 0) {
                        newRow[index] = row[j];
                    } else if (newRow[index] == row[j]) {
                        newRow[index] *= 2;
                        index--;
                    } else {
                        index--;
                        newRow[index] = row[j];
                    }
                }
            }
            matrix[i] = newRow;
        }
        addNumber();
    }

    // 将矩阵向上移动
    private void moveUp() {
        for (int j = 0; j < 4; j++) {
            int[] column = new int[4];
            for (int i = 0; i < 4; i++) {
                column[i] = matrix[i][j];
            }
            int[] newColumn = new int[4];
            int index = 0;
            for (int i = 0; i < 4; i++) {
                if (column[i] != 0) {
                    if (newColumn[index] == 0) {
                        newColumn[index] = column[i];
                    } else if (newColumn[index] == column[i]) {
                        newColumn[index] *= 2;
                        index++;
                    } else {
                        index++;
                        newColumn[index] = column[i];
                    }
                }
            }
            for (int i = 0; i < 4; i++) {
                matrix[i][j] = newColumn[i];
            }
        }
        addNumber();
    }

    // 将矩阵向下移动
    private void moveDown() {
        for (int j = 0; j < 4; j++) {
            int[] column = new int[4];
            for (int i = 0; i < 4; i++) {
                column[i] = matrix[i][j];
            }
            int[] newColumn = new int[4];
            int index = 3;
            for (int i = 3; i >= 0; i--) {
                if (column[i] != 0) {
                    if (newColumn[index] == 0) {
                        newColumn[index] = column[i];
                    } else if (newColumn[index] == column[i]) {
                        newColumn[index] *= 2;
                        index--;
                    } else {
                        index--;
                        newColumn[index] = column[i];
                    }
                }
            }
            for (int i = 0; i < 4; i++) {
                matrix[i][j] = newColumn[i];
            }
        }
        addNumber();
    }

    // 判断游戏是否结束
    private boolean isGameOver() {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (matrix[i][j] == 0) {
                    return false;
                }
                if (i > 0 && matrix[i][j] == matrix[i - 1][j]) {
                    return false;
                }
                if (j > 0 && matrix[i][j] == matrix[i][j - 1]) {
                    return false;
                }
                if (i < 3 && matrix[i][j] == matrix[i + 1][j]) {
                    return false;
                }
                if (j < 3 && matrix[i][j] == matrix[i][j + 1]) {
                    return false;
                }
            }
        }
        return true;
    }
    private void clear() {
        try {

            Robot r = new Robot();
            r.keyPress(KeyEvent.VK_CONTROL);             // 按下Ctrl键
            r.keyPress(KeyEvent.VK_R);                    // 按下R键
            r.keyRelease(KeyEvent.VK_R);                  // 释放R键
            r.keyRelease(KeyEvent.VK_CONTROL);            // 释放Ctrl键
            r.delay(100);
        }catch (Exception e){

        }

    }
    // 打印矩阵
    private void printMatrix() {
        clear();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void main(String[] args) throws IOException {
        Game2048 game2048 = new Game2048();
        game2048.printMatrix();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int keyCode;
        while (!game2048.isGameOver()) {

            keyCode = reader.read();
            if (keyCode==119){
                game2048.moveUp();
                game2048.printMatrix();
            }else if (keyCode==115){
                game2048.moveDown();
                game2048.printMatrix();
            }else if (keyCode==97){
                game2048.moveLeft();
                game2048.printMatrix();
            }else if (keyCode==100){
                game2048.moveRight();
                game2048.printMatrix();

            }

            // 如果用户输入了方向键，则执行相应的移动操作
            // 如果游戏结束，则退出循环
        }
        System.out.println("Game Over!");
    }
}
